(require 'cdlatex)
(load "auctex.el" nil t t)
(load "preview-latex.el" nil t t)
;;(TeX-load-hack)
;;(autoload 'TeX-load-hack emax-tex-site)
;; (add-hook 'reftex-mode-hook '(lambda()
;;                                (progn (define-key reftex-mode-map "\C-c0" reftex-reference))))

;;;;;;;;;;;;;;;;;;;;;basic tex setting;;;;;;;;;;;;;;;;;;;;;;;;;;
(put 'LaTeX-hide-environment 'disabled nil)
(setq-default TeX-master nil)
(setq TeX-auto-save t) 
(setq TeX-parse-self t)
(setq TeX-auto-untabify t)
(setq LaTeX-command 'xelatex)
(setq preview-scale-function 1.2)
(setq TeX-engine-alist
      '((xelatex "XeLaTeX" "xelatex" "xelatex" "xelatex" "xetex")))
(setq LaTeX-command-style (quote (("" "%(latex) %S%(PDFout)"))))
(setq LaTeX-document-regexp "document\\|CJK\\*?")
(setq TeX-engine 'xelatex)
;; (setq LaTeX-section-hook
;;       '(LaTeX-section-heading
;;         LaTeX-section-title
;;         LaTeX-section-toc
;;         LaTeX-section-section
;;         LaTeX-section-label))
(setq TeX-fold-env-spec-list
      (quote (("[figure]" ("figure"))
              ("[table]" ("table"))
              ("[itemize]"("itemize"))
              ("[overpic]"("overpic"))
              ("[equation]"("equation"))
              ("[multline]"("multline")))))

(setq TeX-output-view-style (quote (("^dvi$" "^letterpaper$" "%(o?)xdvi %dS -paper us %d") ("^pdf$" "." "acroread %o %(outpage)") ("^html?$" "." "chromium --enable-plugins %o"))))
(setq TeX-view-program-list (quote (("Evince" ("acroread" (mode-io-correlate " -expand") " %o")))))
;;;;;;;;;;;;;;;;ref setting;;;;;;;;;;;;;;;;;;;;;;;
(setq reftex-plug-into-AUCTeX t)
(setq reftex-label-alist '(AMSTeX))
(setq reftex-enable-partial-scans t)
(setq reftex-save-parse-info t)
(setq reftex-use-multiple-selection-buffers t)
(autoload 'reftex-mode "reftex"
  "RefTeX Minor Mode" t)
(autoload 'turn-on-reftex "reftex"
  "RefTeX Minor Mode" nil)
(autoload 'reftex-citation "reftex-cite"
  "Make citation" nil)
(autoload 'reftex-index-phrase-mode
  "reftex-index" "Phrase mode" t)
(setq reftex-toc-split-windows-horizontally t)
(setq reftex-toc-split-windows-fraction 0.2)
(setq reftex-ref-style-alist (quote (("Default" t (("\\ref" 114) ("\\pageref" 112) ("\\myref" 109) ("\\eqref" 101))) ("Varioref" "varioref" (("\\vref" 118) ("\\vpageref" 103) ("\\Vref" 86) ("\\Ref" 82))) ("Fancyref" "fancyref" (("\\fref" 102) ("\\Fref" 70))) ("Hyperref" "hyperref" (("\\autoref" 97) ("\\autopageref" 117))))))
(setq reftex-revisit-to-follow t
      reftex-auto-recenter-toc t)
(setq reftex-section-levels
      '(("part" . 0) ("chapter" . 1) ("section" . 2) ("subsection" . 3)
        ("frametitle" . 4) ("subsubsection" . 4) ("paragraph" . 5)
        ("subparagraph" . 6) ("addchap" . -1) ("addsec" . -2)))

;;;;;;;;;;;;;;;;;;;;  bibtex setting  ;;;;;;;;;;;;;;;;;;;;
(setq bibtex-autokey-names 1
      bibtex-autokey-names-stretch 1
      bibtex-autokey-name-separator "-"
      bibtex-autokey-additional-names "-et.al."
      bibtex-autokey-name-case-convert 'identity
      bibtex-autokey-name-year-separator "-"
      bibtex-autokey-titlewords-stretch 0
      bibtex-autokey-titlewords 0
      bibtex-maintain-sorted-entries 'plain
      bibtex-entry-format '(opts-or-alts numerical-fields))


(defun my-preview-clearout ()
  "Clearout any preview at point."
  (interactive)
  (let ((start (max (point-min) (1- (point))))
        (end (min (point-max) (1+ (point))))
        (currenv))
    (if (overlays-in (or start (point-min))
                         (or end (point-max)))
        (preview-clearout start end)
      (progn
        (setq currenv (LaTeX-current-environment))
        (when (texmathp)
          (if (not (string= currenv "document"))
              (preview-environment 1)
            (progn
              (while (not (eq (preceding-char) ?$)) (forward-char -1))
              (setq start (1- (point)))
              (while (not (eq (following-char) ?$)) (forward-char 1))
              (setq end (1+ (point)))
              (preview-region start end))))))))

(add-hook 'LaTeX-mode-hook '(lambda ()
                            (progn (LaTeX-install-toolbar)
                                   (TeX-global-PDF-mode)
                                   (LaTeX-math-mode)
                                   (turn-off-auto-fill)
                                   (turn-on-cdlatex)
                                   (imenu-add-menubar-index)
                                   (TeX-fold-mode)
                                   (turn-on-reftex)
                                   (outline-minor-mode)
                                   (local-set-key (kbd "C-<tab>") 'TeX-complete-symbol)
                                   (local-set-key (kbd "C-c C-c") 'my-preview-clearout)
                                   (local-set-key (kbd "C-z 0") 'reftex-reference)
                                   (local-set-key (kbd "C-x C-x") 'TeX-command-master)
                                   )))

(setq LaTeX-math-list '(
    (?1 "ew" "Dimilar Symbol" ) 
    (?2 "code" "Dimilar Symbol" ) 
    (?3 "annotation" "Dimilar Symbol" ) 
    (?4 "mnote" "Dimilar Symbol" ) 
    (?5 "note" "Dimilar Symbol" ) 
    (?6 "cite" "Dimilar Symbol" ) 
    (?7 "nr" "Dimilar Symbol" ) 
    (?8 "todo" "Dimilar Symbol" ) 
    (?9 "prime" "Dimilar Symbol" 8242)
    (?@ "times " "Dimilar Symbol" ) 
    (?O "cdot " "Dimilar Symbol" )
    (?= "equiv " "Dimilar Symbol" )
    (?\C-a "approx " "Dimilar Symbol" )
    ("o p" "oplus " "Dimilar Symbol" )
    ("o m" "ominus " "Dimilar Symbol" )
    ("o t" "otimes " "Dimilar Symbol" )
    ("o s" "oslash " "Dimilar Symbol" )
    ("o o" "odot " "Dimilar Symbol" )
    ("o n" "neq " "Dimilar Symbol" )
    ("<left>" "cdots" "Dimilar Symbol" ) 
    ("<right>" "ldots" "Dimilar Symbol" ) 
    ("<up>" "vdots" "Dimilar Symbol" ) 
    ("<down>" "ddots" "Dimilar Symbol" ) 
    ))
(setq cdlatex-paired-parens "$[{(（《“『【")
;;;;;;;;;;;;;;;;;;;;;;junk;;;;;;;;;;;;;;;;;;;;;;
;;(add-to-list  'TeX-command-list
;;("XeLaTeX" "%'xelatex%(mode)%' %t" TeX-run-TeX nil t))
;; (setq LaTeX-command "xelatex -output-driver=\"xdvipdfmx -q -E\"")
;;(setq preview-transparent-border 2)
;;(setq preview-transparent-color '(highlight :background))
;; (defun flymake-get-tex-args (file-name)
;;   (list "xelatex" (list "-file-line-error" "-draftmode" "-interaction=nonstopmode" file-name)))
;; (setq cdlatex-math-symbol-alist '(( ?2  ("\\note"                 ))))
